/* 
 *   Date       Developer   Description
 *   
 *
 *   Classname:  zzz.test.component.bean.Test01
 * 
 *   Date:   2014-3-11
 * 
 *   The source code contained in this listing is proprietary to
 *   HLJ POST YITONG INFO-NET CO.,LTD.
 * 
 *   Unauthorized copying, adaptation, distribution, use, or display
 *   is strictly prohibited.
 *   This software is Copyright 2014 HLJ POST YITONG INFO-NET CO.,LTD.
 */
package com.titan.demo.camel.sample.component.bean;

import java.util.Date;

import org.apache.camel.CamelContext;
import org.apache.camel.ProducerTemplate;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.mock.MockEndpoint;
import org.apache.camel.impl.DefaultCamelContext;

import com.titan.core.utils.data.FormatUtils;
import com.titan.demo.camel.utils.CamelUtils;


/**
 *
 *
 * @author Administrator
 *
 */
public class Test01 {
    public static void main(String[] args) {
        CamelContext context = null;
        ProducerTemplate template = null;
        try {
            context = new DefaultCamelContext();
            context.addRoutes(new RouteBuilder() {
                public void configure() {
                    from("direct:a").bean(Bean01.class, "doSomething1").to(CamelUtils.log("a")).to("mock:a");
                    from("direct:b").bean(Bean01.class, "doSomething2").to(CamelUtils.log("a")).to("mock:a");
                    from("direct:c").bean(Bean01.class, "changeSomething").to(CamelUtils.log("a")).to("mock:a");
                    from("direct:d").bean(Bean01.class, "doNothing").to(CamelUtils.log("a")).to("mock:a");
                }
            });
            
            template = context.createProducerTemplate();
            template.start();
            context.start();
            
            MockEndpoint mock = context.getEndpoint("mock:a", MockEndpoint.class);
            mock.expectedMessageCount(1);
            mock.expectedBodiesReceived("Hello World");
            template.sendBody("direct:a", "Hello World");
            mock.assertIsSatisfied();
            
            mock.reset();
            mock.expectedMessageCount(1);
            Date date = new Date();
            mock.expectedBodiesReceived(FormatUtils.dateToString(date, "yyyy-MM-dd"));
            template.sendBody("direct:b", date);
            mock.assertIsSatisfied();
            
            mock.reset();
            mock.expectedMessageCount(1);
            mock.message(0).body().isNull();
            template.sendBody("direct:a", null);
            mock.assertIsSatisfied();

            mock.reset();
            mock.expectedBodiesReceived("Bye World");
            template.sendBody("direct:c", "Hello World");
            mock.assertIsSatisfied();

            mock.reset();
            mock.expectedMessageCount(1);
            mock.message(0).body().isNull();
            template.sendBody("direct:c", "");
            mock.assertIsSatisfied();

            mock.reset();
            mock.expectedMessageCount(1);
            mock.message(0).body().isNull();
            template.sendBody("direct:c", null);
            mock.assertIsSatisfied();
            
            mock.reset();
            mock.expectedBodiesReceived("Hello World");
            template.sendBody("direct:d", "Hello World");
            mock.assertIsSatisfied();

            mock.reset();
            mock.expectedBodiesReceived("");
            template.sendBody("direct:d", "");
            mock.assertIsSatisfied();

            mock.reset();
            mock.expectedMessageCount(1);
            mock.message(0).body().isNull();
            template.sendBody("direct:d", null);
            mock.assertIsSatisfied();
            
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            CamelUtils.close(template);
            CamelUtils.close(context);
        }
    }

}

